package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class CjdbyNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.cjdby.net/.*/[0-9]+-[0-9]+-[0-9]+/military-[0-9]+.html",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		ParseTool tool=new ParseTool(doc);
		tool.addParseLogic(new ParseLogic("div.news_info h3"));
		title=tool.parse();

		tool.addParseLogic(new ParseLogic("div.new_box"));
		content=HtmlCleaner.getContentHtml(url ,doc.select("div.new_box"));

		author= JSoupUtils.matchAuthor(doc, "来源：");

		tool.addParseLogic(new ParseLogic("div.other_info"));
		date=tool.parseDate();
		news.setTitle(title);
		news.setContent(content);
		news.setDate(date);

		news.setAuthor(author);
		return news;
	}

	public static void main(String[] args) {
		CjdbyNewsAnalyse analyse=new CjdbyNewsAnalyse();
		String url="http://www.cjdby.net/junbeidongtai/2014-04-04/military-6322.html";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}

}
